package com.gxuwz.zhangjiahui.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.zhangjiahui.entity.College;
import com.gxuwz.zhangjiahui.entity.Instructor;
import com.gxuwz.zhangjiahui.service.ICollegeService;
import com.gxuwz.zhangjiahui.service.IInstructorService;
import org.apache.ibatis.annotations.Param;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangjiahui
 * @since 2021-06-20
 */
@Controller
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    private ICollegeService iCollegeService;


    /**
     * 查询全部信息
     * @param modelAndView
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @Pointcut
    @ResponseBody
    @RequestMapping(value = "/findCollege")
    public ModelAndView findCollege(ModelAndView modelAndView, Page page, Integer pageNumber, College college,
                                    @Param("collegeId") String collegeId,
                                    HttpServletRequest request, HttpServletResponse response) {
        if(request.getSession().getAttribute("nowResult") != null){
            if (!request.getSession().getAttribute("nowResult").equals("now")) {
                request.getSession().setAttribute("result", "");
            }
            request.getSession().setAttribute("nowResult", "");
        }
        // 可以通过 wrapper 进行筛选!!!
        QueryWrapper<College> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("college_id");
        // 进行模糊查询!!!
        if(collegeId != null && collegeId != ""){
            wrapper.like("college_id", collegeId);
            request.getSession().setAttribute("collegeId", collegeId);
        }else {
            request.getSession().setAttribute("collegeId", "");
        }
        // Current,页码 + Size,每页条数
        if(pageNumber == null){
            page.setCurrent(1);
        }else {
            page.setCurrent((long)pageNumber);
        }
        // 默认每页6行数据！
        page.setSize(6);
        // 调用分页查询方法！!
        IPage<College> collegeIPage = iCollegeService.selectCollegePage(page, wrapper);
        // 存放page，内有当前页数
        modelAndView.addObject("page", page);
        System.out.println("总条数"+collegeIPage.getTotal());
        System.out.println("总页数"+collegeIPage.getPages());
        // 存放总页数
        modelAndView.addObject("pages", (int)collegeIPage.getPages());
        // 存放一个数组用来让foreach遍历
        int[] pagesList = new int[(int)collegeIPage.getPages()];
        for(int i=0; i< (int)collegeIPage.getPages(); i++){
            pagesList[i] = i+1;
        }
        modelAndView.addObject("pagesList", pagesList);
        modelAndView.addObject("numberPages", collegeIPage.getTotal());
        List<College> collegeList = collegeIPage.getRecords();
        System.out.println("collegeList = "+collegeList);
        modelAndView.addObject("collegeList", collegeList);

        modelAndView.setViewName("college/college_list");
        return modelAndView;
    }



    /**
     * 添加/修改 信息
     * @param college
     * @return
     */
    @PostMapping(value = "/addEditCollege")
    public ModelAndView addEditCollege(College college, HttpServletRequest request, ModelAndView modelAndView) {
        College college1 = iCollegeService.findCollegeById(college);
        System.out.println("college = " + college);
        System.out.println("college1 = " + college1);
        // 新增信息
        if(college1 == null){
            System.out.println("进入新增");
            try {
                iCollegeService.addCollege(college);
                request.getSession().setAttribute("result", "addTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "addFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        // 修改用户信息
        if(college1 != null){
            System.out.println("进入修改");
            try {
                iCollegeService.updateCollegeById(college);
                request.getSession().setAttribute("result", "editTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "editFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        modelAndView.setViewName("redirect:/college/findCollege");
        return modelAndView;
    }



    /**
     * 删除信息
     * @param collegeId
     * @return
     */
    @GetMapping(value = "/deleteCollege")
    public ModelAndView deleteCollege(HttpServletRequest request, ModelAndView modelAndView,
                             @Param("collegeId") String collegeId) {
        College college = new College();
        college.setCollegeId(collegeId);
        try {
            iCollegeService.deleteCollegeById(college);
            request.getSession().setAttribute("result", "deleteTrue");
            request.getSession().setAttribute("nowResult", "now");
        }catch (Exception e){

        }
        modelAndView.setViewName("redirect:/college/findCollege");
        return modelAndView;
    }

}
